package com.dcbeida.fragment.salsesFragment;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.TextureView;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.activity.sales.StoresalesPresenter;
import com.dcbeida.bean.PatientList;
import com.dcbeida.bean.Sales;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.callback.JsonCallback;
import com.dcbeida.model.ModelUtils;
import com.dcbeida.utils.JsonGet;
import com.dcbeida.utils.Utils;

import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by 150410 on 2017/8/31.
 */

public class SalsesPersenter implements SalsesContract.Persent {
    private final ModelUtils model;
    SalsesContract.View view;
    Activity activity;


    //-------筛选条件----------

    //病人的id
    private int patientid;

    //开始日期
    private String begindate = "";

    //结束日期
    private String enddate = "";

    // 订单id
    private String orderid = "";

    //药品id
    private String drugid = "";

    private int orderstatus = 0;
    //-------筛选条件----------


    public SalsesPersenter(SalsesContract.View view, Activity activity) {
        this.view = view;
        this.activity = activity;
        model = ModelUtils.newInstance();
    }

    @Override
    public void start() {

    }

    public void setBegindate(String begindate) {
        this.begindate = begindate;
    }

    public void setEnddate(String enddate) {
        this.enddate = enddate;
    }

    public void setOrderid(String orderid) {
        this.orderid = orderid;
    }


    public void setDrugid(String drugid) {
        this.drugid = drugid;
    }

    public void setOrderstatus(int orderstatus) {
        this.orderstatus = orderstatus;
    }

    public void setPatientid(int patientid) {
        this.patientid = patientid;
    }

    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };

    @Override
    public void getList(String hospitalid, int pageindex, int pagesize) {
        if (TextUtils.isEmpty(begindate) || TextUtils.isEmpty(enddate)) {
            begindate = Utils.getNowTimeBeforeOneYear(-1);
            enddate = Utils.getNowTimeBeforeOneYear(1);
        }
        if (begindate.equalsIgnoreCase(enddate)) {
            begindate += " 00:00:00";
            enddate += " 23:59:59";
        }
        JsonGet jg = new JsonGet().putopt("uid", MyApplication.user.getUid())
                .putopt("shopid", MyApplication.user.getShopid())
                .putopt("patientid", patientid)
                .putopt("hospitalid", hospitalid)
                .putopt("begindate", begindate)
                .putopt("enddate", enddate)
                .putopt("orderid", orderid)
                .putopt("drugid", drugid)
                .putopt("orderstatus", orderstatus)
                .putopt("pageindex", pageindex)
                .putopt("pagesize", pagesize);
        String json = jg.tostring();
        model.getDate("getorderlistnew", json, new SalsesPersenter.GetListCallBack(activity, Sales.class, lis));
    }

    @Override
    public void getPatientInfo(int type, String keyword) {

        JsonGet jg = new JsonGet().putopt("type", type)
                .putopt("keyword", keyword);
        String json = jg.tostring();
        model.getDate("querypatient", json, new DialogCallback<PatientList>(activity, PatientList.class,
                new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        model.getCancel();
                    }
                }) {
            @Override
            public void onResponse(PatientList data) {
                if (data != null)
                    view.showPatientList(data);
            }
        });
    }

    public class GetListCallBack<T> extends DialogCallback<T> {
        public GetListCallBack(Activity activity, Type type, DialogInterface.OnCancelListener lis) {
            super(activity, type, lis);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (((Sales) data).getList().size() >= 0) {
                    view.showList(((Sales) data).getList());
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showView(false);
            view.showTelTip("数据未请求成功,请检查网络...");
        }
    }
}
